package com.yyh.fanshe;

import java.lang.reflect.Field;

public class Demo2 {
    public static void main(String[] args) throws Exception {
        //1.获取Class对象
        Class.forName("com.yyh.fanshe.User");
        //Field对应的获取功能
        //1.根据名称获取对应的Field对象
        Field agefield = User.class.getField("age");
        System.out.println("agefield = " + agefield);
        System.out.println("------------------------------------------------");
        //2.根据名称获取对应的Field对象,可以获取私有的Field对象
        Field namefield = User.class.getDeclaredField("name");
        System.out.println("namefield = " + namefield);
        System.out.println("------------------------------------------------");
        //3.获取所有的public修饰的Field对象
        Field[] fields = User.class.getFields();
        for (Field field : fields) {
            System.out.println("field = " + field);
        }
        System.out.println("------------------------------------------------");
        //4.获取所有的Field对象，包括private修饰的
        Field[] declaredFields = User.class.getDeclaredFields();
        for (Field field : declaredFields) {
            System.out.println("field = " + field);
        }

    }
}

